package com.example.backend.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.backend.entity.User;
import com.example.backend.entity.Word;
import com.example.backend.mapper.UserMapper;
import com.example.backend.mapper.WordMapper;
import com.example.backend.service.WordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class WordServiceImpl implements WordService {
    @Autowired
    private WordMapper wordMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public Map<String, Object> findWord(String wordSpell) {
        Map<String, Object> map = new HashMap<String, Object>();
        QueryWrapper<Word> wrapper = new QueryWrapper<>();
        wrapper.eq("word_spell", wordSpell);
        Word word = wordMapper.selectOne(wrapper);
        if(word != null){
            map.put("wordId", word.getWord_id());
            map.put("wordSpell", word.getWord_spell());
            map.put("paraphrase", word.getParaphrase());
            map.put("soundmark", word.getSoundmark());
            return map;
        }
        return null;
    }

    @Override
    public Map<String, Object> findWordByAdmin(String wordSpell) {
        Map<String, Object> map = new HashMap<String, Object>();
        QueryWrapper<Word> wrapper = new QueryWrapper<>();
        wrapper.eq("word_spell", wordSpell);
        Word word = wordMapper.selectOne(wrapper);
        if(word != null){
            map.put("wordId", word.getWord_id());
            map.put("wordSpell", word.getWord_spell());
            return map;
        }
        return null;
    }

    @Override
    public Map<String, String> getWordSpecification(int wordId) {
        Map<String, String> map = new HashMap<>();
        Word word = wordMapper.selectById(wordId);
        if(word != null) {
            map.put("wordSpell", word.getWord_spell());
            map.put("paraphrase", word.getParaphrase());
            map.put("soundmark", word.getSoundmark());
            return map;
        }
        return null;
    }

    @Override
    public boolean modifyWordSpecification(int wordId, Map<String, String> params) {
        Word word = wordMapper.selectById(wordId);
        String wordSpell = params.get("wordSpell");
        String paraphrase = params.get("paraphrase");
        String soundmark = params.get("soundmark");
        if(wordSpell != null){
            word.setWord_spell(wordSpell);
        }
        if(paraphrase != null){
            word.setParaphrase(paraphrase);
        }
        if(soundmark != null){
            word.setSoundmark(soundmark);
        }
        wordMapper.updateById(word);
        if((wordSpell != null && !word.getWord_spell().equals(wordSpell)) || (paraphrase != null && !word.getParaphrase().equals(paraphrase)) || (soundmark != null && !word.getSoundmark().equals(soundmark)))
            return false;
        return true;
    }

    @Override
    public int deleteWord(int wordId, int adminId, String password) {
        User user = userMapper.selectById(adminId);
        if(!user.getPassword().equals(password)){
            return 3;
        }
        Word word = wordMapper.selectById(wordId);
        if(word == null){
            return 1;
        }
        wordMapper.deleteById(wordId);
        if (wordMapper.selectById(wordId) == null){
            return 0;
        }
        return 2;
    }

    @Override
    public int addWord(Map<String, String> params) {
        String wordSpell = params.get("wordSpell");
        String paraphrase = params.get("paraphrase");
        String soundmark = params.get("soundmark");
        QueryWrapper<Word> wrapper = new QueryWrapper<>();
        wrapper.eq("word_spell", wordSpell);
        if(wordMapper.selectOne(wrapper) != null)
            return 1;
        Word word = new Word();
        word.setWord_spell(wordSpell);
        word.setParaphrase(paraphrase);
        word.setSoundmark(soundmark);
        word.setDeleted(0);
        wordMapper.insert(word);
        int wordId = word.getWord_id();
        if(wordMapper.selectById(wordId) != null){
            return 0;
        }
        return 2;
    }
}
